package com.syq.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.syq.annotation.RequiresPermissionsDesc;
import com.syq.pojo.DtsPermission;
import com.syq.pojo.DtsRole;
import com.syq.service.DtsPermissionService;
import com.syq.service.DtsRoleService;
import com.syq.util.Permission;
import com.syq.util.PermissionUtil;
import com.syq.util.ResponseUtil;
import com.syq.util.UUID;
import com.syq.vo.CatVo;
import com.syq.vo.PermUpdateVo;
import com.syq.vo.PermVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @ClassName: AdminRoleController
 * @Description: TODO
 * @Create by: syq
 * @Date: 2022/7/25 15:07
 */

@RestController
@RequestMapping("/admin/role")
@CrossOrigin
public class AdminRoleController {

    @Autowired
    private DtsRoleService dtsRoleService;

    @Autowired
    private ApplicationContext context;

    @Autowired
    private DtsPermissionService dtsPermissionService;

    //角色
    @GetMapping("/options")
    public Object options(){

        List<CatVo> data=dtsRoleService.findAllRole();

        return ResponseUtil.ok(data);
    }

    //角色列表
    @RequiresPermissionsDesc(menu = {"系统管理","角色管理"},button = "查询")
    @RequiresPermissions(value = "admin:role:list")
    @GetMapping("/list")
    public Object list(@RequestParam(defaultValue = "1")Integer page,
                       @RequestParam(defaultValue = "10")Integer limit,
                       @RequestParam(defaultValue = "add_time")String sort,
                       @RequestParam(defaultValue = "desc")String order,String name){
        IPage<DtsRole> roleIPage=dtsRoleService.findByPage(page,limit,name,sort,order);
        Map<String,Object> map=new HashMap<>();
        map.put("total",roleIPage.getTotal());
        map.put("items",roleIPage.getRecords());
        return ResponseUtil.ok(map);
    }

    //角色添加
    @RequiresPermissionsDesc(menu = {"系统管理","角色管理"},button = "添加")
    @RequiresPermissions(value = "admin:role:create")
    @PostMapping("/create")
    public Object addRole(@RequestBody DtsRole dtsRole){
        dtsRole.setAddTime(new Date());
        dtsRole.setUpdateTime(new Date());
        dtsRoleService.addRole(dtsRole);
        return ResponseUtil.ok();
    }

    @RequiresPermissionsDesc(menu = {"系统管理","角色管理"},button = "修改")
    @RequiresPermissions(value = "admin:role:update")
    @PostMapping("/update")
    public Object updateRole(@RequestBody DtsRole dtsRole){
        dtsRole.setUpdateTime(new Date());
        dtsRoleService.update(dtsRole);
        return ResponseUtil.ok();
    }

    @RequiresPermissionsDesc(menu = {"系统管理","角色管理"},button = "删除")
    @RequiresPermissions(value = "admin:role:delete")
    @GetMapping("/delete")
    public Object delete(@RequestBody DtsRole dtsRole){
        dtsRoleService.delete(dtsRole.getId());
        return ResponseUtil.ok();
    }

    @RequiresPermissionsDesc(menu = {"系统管理","角色管理"},button = "授权查询")
    @RequiresPermissions(value = "admin:role:permissions")
    @GetMapping("/permissions")
    public Object permissions(Integer roleId){
        Map<String,Object> map=new HashMap<>();
        List<Permission> permissions = PermissionUtil.listPermission(context, "com.syq");
        List<PermVo> systemPermissions = PermissionUtil.listPermVo(permissions);

        Set<String> assignedPermissions = dtsPermissionService.findById(roleId);

        map.put("systemPermissions",systemPermissions);
        map.put("assignedPermissions",assignedPermissions);
        return ResponseUtil.ok(map);
    }

    @RequiresPermissionsDesc(menu = {"系统管理","角色管理"},button = "授权操作")
    @RequiresPermissions(value = "admin:role:permissionsUpdate")
    @PostMapping("/permissions")
    public Object permissionsUpdate(@RequestBody PermUpdateVo permUpdateVo){
        dtsPermissionService.updatePerm(permUpdateVo);
        return ResponseUtil.ok();
    }
}
